/*
 * @Author: 何善万 swhe@che300.com
 * @Date: 2023-11-06 10:34:55
 * @LastEditors: 何善万 swhe@che300.com
 * @LastEditTime: 2024-12-19 16:09:48
 * @FilePath: \my-notion-redux\src\routers.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React, { Component, lazy } from 'react';
import { Routes, Route, HashRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import {useTranslation} from 'react-i18next'
import {i18_LANGUAGE, LANGUAGE_TYPE, THEME_COLOR_TYPE} from '@tools/constants'
import {storageData} from '@tools/utils';
import i18n from 'i18next'
import {debounce} from 'lodash'
import {setCommonFunc} from './redux/slices/commonFuns'
import {setGlobalCommon} from './redux/slices/global'
import store from './redux/store';
import Job from './pages/job'
import './index.less'




class Routers extends Component{
  constructor(props) {
    super(props);
    this.state = {};
  }
  componentDidMount() {
    let theme = storageData(THEME_COLOR_TYPE);
    this.setThemeColor(theme || 'default');
    store.dispatch(setCommonFunc({
      setThemeColor: this.setThemeColor,
      changeLanguage: this.changeLanguage
    }))
    window.addEventListener('resize', debounce(this.handleResize, 100, {
      leading: false,
      trailing: true
    }));
  }

 handleResize =  () => {
    store.dispatch(setGlobalCommon({
      isChangeMedia: window.innerWidth > 1080
    }))
  }

  setThemeColor = (theme) => {
    document.documentElement.setAttribute('data-theme', theme);
    store.dispatch(setGlobalCommon({
      theme
    }))
    
    storageData(THEME_COLOR_TYPE, theme)
  }

    // 设置i18n语言
    changei18nLanguage = (value) => {
      i18n.changeLanguage(value).then(() => {
        // 缓存值
        storageData(LANGUAGE_TYPE, value);
        store.dispatch(setGlobalCommon({
          language: value
        }))
      });
    };
    // 设置antd语言包
    changeLanguage = (value) => {
      const langeiageStr = i18_LANGUAGE[value];
      // 修改i18语言包
      this.changei18nLanguage(value);
   
    };

  render() {
    return (
      <Provider store={store}>
        <HashRouter>
            <Routes>
              <Route path="/" element={<Job />} />
              <Route path="/test" element={<div>test !!!</div>} />
              <Route path="/*" element={<div>error page !!!</div>} />
            </Routes>
        </HashRouter>
      </Provider>
      
    );
  }
}

export default Routers;
